package com.example.basic.web.controller.pharmZone;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.basic.core.result.Result;
import com.example.basic.core.result.ResultGenerator;
import com.example.basic.custom.Log;
import com.example.basic.web.model.pharmZone.PharmZone;
import com.example.basic.web.service.pharmZone.PharmZoneService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@Api(value  = "分区管理", tags = {"分区管理"})
@RestController
@RequestMapping("/api/v1/byg/pharmZone")
public class PharmZoneController {

    @Resource
    private PharmZoneService pharmZoneService;

    @Log(title = "分区管理", description = "新增或修改")
    @ApiOperation(value = "新增或修改", notes = "新增或修改")
    @PostMapping("/addOrUpdate")
    public Result<PharmZone> addOrUpdate(@ApiParam(name = "pharm", value = "模型", required = true) @RequestBody PharmZone pharm) {
        return ResultGenerator.genSuccessResult(pharmZoneService.addOrUpdate(pharm));
    }

    @Log(title = "分区管理", description = "详情")
    @ApiOperation(value = "详情", notes = "详情")
    @PostMapping("/detail")
    public Result<PharmZone> detail(@ApiParam(name = "zoneCode", value = "分区编码") @RequestParam String zoneCode) {
        return ResultGenerator.genSuccessResult(pharmZoneService.detail(zoneCode));
    }

    @Log(title = "分区管理", description = "删除")
    @ApiOperation(value = "删除", notes = "删除")
    @PostMapping("/delete")
    public Result<Integer> delete(@ApiParam(name = "zoneCode", value = "分区编码") @RequestParam String zoneCode ) {
        return ResultGenerator.genSuccessResult(pharmZoneService.delete(zoneCode));
    }

    @Log(title = "分区管理", description = "启用/禁用")
    @ApiOperation(value = "启用/禁用", notes = "启用/禁用")
    @PostMapping("/enableOrDisable")
    public Result<Integer> enableOrDisable(@ApiParam(name = "zoneCode", value = "分区编码") @RequestParam String zoneCode) {
        return ResultGenerator.genSuccessResult(pharmZoneService.enableOrDisable(zoneCode));
    }

    @Log(title = "分区管理", description = "列表")
    @ApiOperation(value = "列表", notes = "列表")
    @PostMapping("/queryList")
    public Result<IPage<PharmZone>> queryList(
            @ApiParam(name = "pharmCode", value = "仓库编码") @RequestParam(required = false) String pharmCode,
            @ApiParam(name = "searchParam", value = "模糊搜索(拼音码,五笔码,名称,编码)") @RequestParam(required = false) String searchParam,
            @ApiParam(name = "isAll", value = "是否查询全部 0-否，1-是") @RequestParam(defaultValue = "0") String isAll,
            @ApiParam(name = "page", value = "页数，list分页的第几页") @RequestParam(defaultValue = "0") Integer page,
            @ApiParam(name = "size", value = "条数，list分页的每页条数") @RequestParam(defaultValue = "0") Integer size) {
        Page<PharmZone> queryPage = new Page<>(page,size);
        return ResultGenerator.genSuccessResult(pharmZoneService.queryList(queryPage,searchParam, isAll,pharmCode));
    }

    @Log(title = "分区管理", description = "仓库下分区")
    @ApiOperation(value = "仓库下分区", notes = "仓库下分区")
    @PostMapping("/queryByPharm")
    public Result<List<PharmZone>> queryByPharm(
            @ApiParam(name = "pharmCode", value = "仓库编码") @RequestParam String pharmCode) {
        return ResultGenerator.genSuccessResult(pharmZoneService.queryByPharm(pharmCode));
    }

    @ApiOperation(value="导入",notes = "导入")
    @PostMapping("/importData")
    public Result importData(
            @ApiParam(name = "file", value = "excel文件") @RequestParam MultipartFile file,
            @ApiParam(name = "pharmCode", value = "仓库编码") @RequestParam String pharmCode){
        return ResultGenerator.genSuccessResult(pharmZoneService.importData(file,pharmCode));
    }

    @Log(title = "数据导出", description = "分区导出")
    @ApiOperation(value="分区导出",notes = "分区导出")
    @GetMapping("/zoneExport")
    public void zoneExport(
            @ApiParam(name = "searchParam", value = "模糊搜索(拼音码,五笔码,名称,编码)") @RequestParam(required = false) String searchParam,
            @ApiParam(name = "isAll", value = "是否查询全部 0-否，1-是") @RequestParam(defaultValue = "0") String isAll,
            @ApiParam(name = "page", value = "页数，list分页的第几页") @RequestParam(defaultValue = "0") Integer page,
            @ApiParam(name = "size", value = "条数，list分页的每页条数") @RequestParam(defaultValue = "0") Integer size,
            HttpServletResponse response) throws IOException {
        Page<PharmZone> queryPage = new Page<>(page, size);
        pharmZoneService.zoneExport(queryPage, searchParam, isAll,response);
    }
}
